package com.it.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.it.biz.UserBiz;
import com.it.pojo.User;
import com.it.pojo.UserExample;

@Controller
public class UserAction {
	
	@Resource
	private UserBiz userBiz;
	
	@RequestMapping("/login.do")
	public String login(String name,String password,Model model,HttpSession session) {
		UserExample userExample = new UserExample();
		userExample.createCriteria().andUsernameEqualTo(name);
		List<User> users = (List<User>) userBiz.selectByExample(userExample);
		if(users!=null&&users.size()>0) {
			if(password.equals(users.get(0).getPassword())) {
				session.setAttribute("user",users.get(0));
				return "main.jsp";
			}else {
				model.addAttribute("msg","密码错误！");
				return "index.jsp";
			}
		}
		model.addAttribute("msg","用户不存在");
		return "index.jsp";
	}
	
	@RequestMapping("/logout.do")
	public String logout(HttpSession session) {
		session.invalidate();
		return "index.jsp";
	}
}
